﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Drawing;

namespace ETFK
{
    /// <summary>
    /// 这个类用来记录自动测试的过程
    /// </summary>
    public class CheckRecord
    {
        private static StringBuilder sb = new StringBuilder();
        public static RichTextBox OutTextBox { get; set; }
        private static Stopwatch stopwatch = new Stopwatch();

        /// <summary>
        /// 添加一条消息，默认为显示为白色
        /// </summary>
        /// <param name="Message">消息</param>
        public static void Add(string Message)
        {
            Add(Color.White, Message);
        }

        /// <summary>
        /// 添加一条消息，并指定显示的颜色
        /// </summary>
        /// <param name="ForeColor">显示的颜色</param>
        /// <param name="Message">消息</param>
        public static void Add(Color ForeColor, string Message)
        {
            sb.AppendLine(Message);
            //当OutTextBox被赋值时，则显示消息
            if (OutTextBox != null)
            {
                OutTextBox.SelectionColor = ForeColor;
                OutTextBox.AppendText(Message + "\r\n");
                OutTextBox.ScrollToCaret();
            }

            Application.DoEvents();
        }

        /// <summary>
        /// 清除消息记录
        /// </summary>
        public static void Clear()
        {
            sb = new StringBuilder();
        }

        /// <summary>
        /// 获得消息记录
        /// </summary>
        /// <returns></returns>
        public static string GetCheckRecord()
        {
            return sb.ToString();
        }

        /// <summary>
        /// 添加测试开始消息。
        /// </summary>
        public static void TestStart()
        {
            Add(Color.Yellow, "".PadLeft(80, '>'));
            Add(Color.Yellow, "开始测试 " + DateTime.Now.ToString());
            Add(Color.Yellow, "".PadLeft(80, '<'));
            stopwatch.Reset();
            stopwatch.Start();
        }

        /// <summary>
        /// 添加测试结束消息
        /// </summary>
        public static void TestEnd()
        {
            stopwatch.Stop();
            Add(Color.Yellow, "".PadLeft(80, '>'));
            Add(Color.Yellow, "结束测试 " + DateTime.Now.ToString() + " 用时：" + ((double)stopwatch.ElapsedMilliseconds / 1000).ToString("0.00") + "秒");
            Add(Color.Yellow, "".PadLeft(80, '<'));
        }

        /// <summary>
        /// 添加测试错误消息
        /// </summary>
        public static void TestError(string Message)
        {
            Add(Color.Red, "无法进行测试(" + Message + ")");
        }
    }
}
